<template>
    <el-dialog class="create-exception-dialog" title="生成异常类" width="85%"
               :visible.sync="visible" :before-close="handleClose">

        <div>
            <template v-for="e in es">
                <h4 v-text="e.name"></h4>

                <p>Exception</p>
                <ac-high-light v-model="e.exception"></ac-high-light>

                <p>Enum</p>
                <ac-high-light v-model="e.enums"></ac-high-light>

                <br><br>
                <hr>
            </template>
        </div>

        <div slot="footer">
            <el-button @click="handleClose">取 消</el-button>
            <el-button type="success" @click="saveClick">保存文件</el-button>
        </div>
    </el-dialog>
</template>

<script type="text/ecmascript-6">
    import exception from '../model/exception';
    import enums from '../model/enums';
    import path from 'path';
    import fs from 'fs';
    import AcHighLight from "../common/HighLightTextArea";

    /**
     * @author 白雨浓
     * @date 2018/1/29 18:54
     *
     * 异常类 窗口
     **/
    export default{
        components: {AcHighLight},
        name: 'AcCreateExceptionDialog',
        props: ['dialogVisible', 'names', 'config',],
        data(){
            return {
                visible: this.dialogVisible,
                es: []/*异常类和枚举类*/
            }
        },
        mounted(){
            this.$nextTick(() => {
                this.names.forEach(name => {
                    this.es.push({
                        name: name,
                        exception: exception.exception(this, name),
                        enums: enums.enums(this, name)
                    })
                });
            })
        },
        methods: {
            saveClick(){
                let projectPath = this.config.projectPath;

                if (projectPath[projectPath.length - 1] !== '/')
                    projectPath += '/';
                projectPath += 'src/main/java/';
                projectPath += this.config.packageName.replace(/\./g, '/');

                if (!fs.existsSync(path.join(projectPath))) {
                    this.$$msg.error('工程目录不存在，请检查基础设置-ProjectPath');
                    return;
                }

                for (const e of this.es) {
                    let clsName = this.$str.toCamel(e.name);
                    clsName = clsName.substring(0, 1).toUpperCase() + clsName.substring(1);

                    this.writeFile(projectPath, `exception`, `${clsName}Exception`, e.exception);
                    this.writeFile(projectPath, `enums`, `${clsName}Enum`, e.enums);
                }
                this.handleClose();
            },
            writeFile(projectPath, packageName, filename, text){
                const filePath = path.join(projectPath + `/${packageName}/`, filename + '.java');

                if (fs.existsSync(filePath)) {
                    this.$notify({
                        title: '提示',
                        message: `${filename}.java 文件已经存在！`,
                        type: 'error',
                        duration: 0
                    });
                    return false;
                }
                fs.writeFileSync(filePath, text);
                return true;
            },
            /** 关闭处理 */
            handleClose(){
                this.visible = false;
                setTimeout(() => this.$emit('update:dialogVisible', false), 300);
            }
        }
    }
</script>

<style lang="stylus" rel="stylesheet/stylus" scoped>
    .create-exception-dialog {
    }
</style>